home *** CD-ROM | disk | FTP | other *** search
/ Amiga Mag HDD Backup / Amiga Mag HDD Backup.zip / Amiga Mag HDD Backup / Alexander.img.bin / Alexander.img / 315 2 files Archive.sit / Any plain-text files / ? Any plain-text file 95 < prev    next >
Text File  |  1995-01-03  |  24KB  |  456 lines

  1. Front Ending C
  2.  
  3.  
  4.     As many of you know, C is a very good higher level language
  5. for writing speedy applications. Typically, however, there are
  6. just a few places within the program code where this speed is
  7. critical, i.e. important from the user perspective. Usually,
  8. handling input from the user through an application's interface
  9. is not one of these critical points. One then might ask, "Could
  10. I build the interface using an interactive authoring tool such as
  11. HELM or CanDo, write the speed critical parts in C, and then
  12. combine the two?" The logic behind this is that an interface is
  13. easier to build with an authoring tool than with C. Even if an
  14. interface creation tool kit that writes C code for you is used,
  15. it is sometimes confusing adding the event handling. In this
  16. article, I show you one way an authoring tool can be used to
  17. build a front end to a speed critical C application. There are
  18. not many applications that are as floating point math intensive
  19. as those that plot the Mandelbrot set and Julia sets. I wrote the
  20. core calculations for such an application in SAS/C 6.50 and then
  21. added a user interface designed in CanDo 2.51. To point out the
  22. speed advantages of writing the calculations in C as opposed to
  23. CanDo's scripting language, I have also written the calculations
  24. in CanDo so a speed comparison can be done.
  25.  
  26. The Mandelbrot Set and Julia Sets
  27.     For those of you unfamiliar with the Mandelbrot set and
  28. Julia sets (where have you been the last 10 years, under a
  29. rock?), I now give a brief explanation. The Mandelbrot set is a
  30. mathematical entity known as a fractal. It can be defined semi-
  31. formally as the set of all points, ci, in the complex plane such
  32. that the value of z in the iteration formula zn+1 = zn2 + ci
  33. remains bounded as n approaches infinity for an initial value of
  34. z (z0) of 0+0i. It can be shown that if the distance of z from
  35. the origin ever exceeds two, then the value of z approaches
  36. infinity as n approaches infinity and is therefore not bounded.
  37.     Julia sets are fractals also and are similar to the
  38. Mandelbrot set. However, there are an infinite number of Julia
  39. sets whereas there is only one Mandelbrot set. There is one
  40. Julia set for each value of ci in the complex plane. The Julia
  41. set for a particular value of ci is the set of all points, z, in
  42. the complex plane such that the value of z in the iteration
  43. formula zn+1 = zn2 + ci remains bounded as n approaches infinity.
  44.     Well, so much for definitions. If you have seen any
  45. Mandelbrot or Julia set plots, you know that the points that make
  46. up the set are usually not the main point of interest; the
  47. points immediately surrounding the set are. Typically, the
  48. points belonging to the set are all one color and the points
  49. nearby are colored differently based on the number of iterations
  50. necessary to cause the distance of the point z from the origin to
  51. exceed two. This results in an image like that shown in Figure
  52. 1, which is the Mandelbrot set. Since the criteria for ceasing
  53. the iterations of the equation is that the distance of the point
  54. from the origin be greater than two, the image consists of a
  55. circle of radius two with inner distorted circles of decreasing
  56. radius. As the number of iterations approaches infinity, the
  57. distorted circles approach the shape of the outline of the
  58. Mandelbrot set. The same holds true for Julia sets. Of course,
  59. any program that actually carried the iterations to infinity
  60. would never end; therefore, a limit is specified. The number of
  61. iterations it takes to cause the distance of z from the origin to
  62. exceed two is known as the dwell of the point ci (z for a Julia
  63. set). The iteration limit is known as the maximum dwell or dwell
  64. limit. All points that allow the equation to be iterated the
  65. number of times specified by the maximum dwell are assumed to be
  66. a member of the Mandelbrot or Julia set being calculated. My
  67. program, CanDoMJ, allows the user to set the maximum dwell at
  68. either 15, 31, 63, or 127 via a menu selection. The larger this
  69. value is, the more accurate the plot will be but the longer it
  70. will take to calculate points close to and in the set. Let's now
  71. take a look at the CanDoMJ program. It is shown in Listing 1.
  72.  
  73. The CanDoMJ Deck
  74.     The CanDoMJ deck consists of two cards: MJSettings and
  75. MJPlotPic. The former is the main user interface where various
  76. settings related to the type of plot to generate can be entered.
  77. This card is the first to appear when the program begins
  78. execution. The latter is used for actually plotting the
  79. Mandelbrot or Julia set.
  80.  
  81. The MJSettings Card
  82.     This card is shown in Figure 2. It consists of six text
  83. fields, one image button, two text buttons, and one menu
  84. containing four menu items. Some of these items also have
  85. subitems associated with them. The card also has
  86. BeforeAttachment and AfterAttachment scripts as well as a local
  87. routine named DrawMJPic.
  88.  
  89. The BeforeAttachment Script for the MJSettings Card
  90.     This script executes just before the card is displayed. It
  91. checks to see if this is the first time the script has been
  92. executed (Invocation=0; all uninitialized variables default to a
  93. value of zero) and, if it is, sets default values for the
  94. maximum dwell, plot resolution, and plot type. These defaults
  95. can be changed via menu selections. The value of Invocation is
  96. set to one so these defaults will not be set again while the
  97. program is running.
  98.  
  99. The AfterAttachment Script for the MJSettings Card
  100.     This script first removes the check mark from all menu
  101. subitems and then adds a check mark to the appropriate subitems
  102. as determined by the current values of MaxDwell, Resolution, and
  103. PlotType. This is necessary since CanDo automatically puts check
  104. marks on the subitems with their Checked property set at design
  105. time. Next, the value of the first argument passed to the card,
  106. Arg1, is checked. If it is equal to "Reset", the six text fields
  107. are filled in with the current values of six variables. If this
  108. is not done, CanDo will automatically fill in the fields with the
  109. default values specified at design time. Finally, all of the text
  110. for the card (see Figure 2) is displayed.
  111.  
  112. The Text Fields
  113.     The six text fields are named LowReal, HighReal, LowImag,
  114. HighImag, JReal, and JImag. The first four fields (on the right
  115. side of the card) are for entering floating point numbers that
  116. define the rectangular plot region on the complex plane. The
  117. default values for these fields are -2.9, 2.9, -2.0, and 2.0,
  118. respectively. The JReal and JImag text fields (on the lower left
  119. corner of the card) are for entering floating point numbers that
  120. define the complex value, ci, that is used when plotting a Julia
  121. set. The default values are 0.5 and 0.0, respectively.
  122.     All six text fields have an OnRelease event script that
  123. simply activates the next field in sequence. This eliminates the
  124. need to activate each field by mouse clicking between entries. Be
  125. aware that the text fields will allow any alphanumeric character
  126. to be entered into the field. It would be nice if INOVAtronics
  127. would add a masked input field object to CanDo or, more
  128. preferably, provide a KeyPress event for field objects. The
  129. latter feature would allow a script to be executed each time the
  130. user presses a key while entering text into a field. This would
  131. allow programmer filtering of user input.
  132.  
  133. The Image and Text Buttons
  134.     The image button is named SeeMJPic and appears on the left
  135. side of the card. Clicking on the button causes the last
  136. generated plot to be displayed. The image for this button is a
  137. brush named CanDoMJButton.br. The OnRelease script for this
  138. button executes the routine DrawMJPic, passing the argument "SEE"
  139. to it.
  140.     The two text buttons are named GOC and GO. They appear in
  141. the lower right corner of the card. The former is used to
  142. generate a plot by calling a C program; the latter is used to
  143. generate the same plot using CanDo code. The OnRelease scripts
  144. for these buttons execute the DrawMJPic routine, passing
  145. arguments of "GOC" and "GO", respectively.
  146.  
  147. The DrawMJPic Routine
  148.     This routine extracts the text from each text field on the
  149. card and assigns their values to six appropriately named global
  150. variables. It then activates the MJPlotPic card, passing to it
  151. the argument that was passed to the routine itself. The content
  152. of the fields must be assigned to variables because the MJPlotPic
  153. card knows nothing of the fields on the MJSettings card but can
  154. access global variables.
  155.  
  156. The PlotInfo Menu
  157.     The MJSettings card has one menu structure named PlotInfo
  158. that contains four menu items. Some of the menu items have
  159. subitems. The menu structure looks like this:
  160.  
  161.     PlotInfo
  162.         Max Dwell
  163.             15
  164.             31
  165.             63
  166.             127
  167.         Resolution
  168.             1
  169.             2
  170.             4
  171.             8
  172.         Plot Type
  173.             Mandelbrot
  174.             Julia
  175.         Default Settings
  176.  
  177. Each selectable menu item or subitem has a shortcut key
  178. associated with it. They are shown in the definition sections of
  179. the menu objects in Listing 1.
  180.     The menu subitems under Max Dwell are mutually exclusive;
  181. only one item can be active at a time. A check mark appears next
  182. to the selected value. However, CanDo does not automatically
  183. handle mutually exclusive menu subitems. It will check the
  184. selected item, but it will not remove the check mark from any of
  185. the other items. This must be done by the programmer. As you can
  186. see from the Occurred scripts of the subitems (named MD15, MD31,
  187. MD63, and MD127), not only is the value of MaxDwell set, but the
  188. non-selected subitems have their check marks removed using the
  189. SetObjectState command. The same procedure is used by the
  190. subitems for Resolution and Plot Type. For the check marks to
  191. appear and disappear properly, be sure that the Checkmark
  192. property of the subitems are set at design time (see Figure 3).
  193.     The definition of maximum dwell has already been given and
  194. will not be discussed further. The meaning of plot type is
  195. obvious. So, let's discuss resolution.
  196.     The value of Resolution can be 1, 2, 4, or 8. When the
  197. value of Resolution is N, then an NxN pixel area will be drawn on
  198. screen for each calculated point in the Mandelbrot or Julia set.
  199. Thus, if Resolution is equal to 4, a 4x4 block of pixels will be
  200. colored for each calculated point. This allows rough but quick
  201. plots to be generated before actually plotting with a resolution
  202. of 1. Figure 4 shows a Julia set plot with a resolution of 4.
  203.     When the Default Settings menu item is selected, the text
  204. fields containing the plot range and ci value are set to their
  205. default values. This is accomplished by reactivating the
  206. MJSettings card without passing the "Reset" argument (see earlier
  207. discussion of the AfterAttachment script).
  208.     When creating a menu structure in CanDo, several levels of
  209. requesters must be filled in to create the menu items and
  210. subitems. These different requesters are shown together in
  211. Figure 3. Stepping through these requesters when creating a
  212. complex menu structure can be confusing. I would like to see
  213. these requesters combined into one well thought out requester.
  214.  
  215. The MJPlotPic Card
  216.     This card consists of a 320x200 32-color window with no
  217. system window objects. Thus, the card appears totally blank.
  218. However, the card does have one area button named NewRange and
  219. one menu structure named Options. The card also has an
  220. AfterAttachment script.
  221.  
  222. The AfterAttachment Script for the MJPlotPic Card
  223.     This script first checks the value of Arg1. If it is equal
  224. to "GO", a plot is generated with CanDo code. If it is equal to
  225. "GOC", a plot is generated using the C program, CanDoMJC. If it
  226. is equal to "SEE", the last generated plot is displayed.
  227.     If CanDo code is to be used to generate a plot, several
  228. variables are initialized, and either a Mandelbrot set or a Julia
  229. set plot is created depending on the value of PlotType. I leave
  230. it to you to follow through the code and see how it implements
  231. the formulas given at the beginning of this article. Several
  232. references are also given at the end of this article.
  233.     If the C program, CanDoMJC, is to be used to generate a
  234. plot, an AmigaDOS command line string is built up using several
  235. string concatenation operations and then executed using the Dos
  236. command. The final command will look something like this:
  237.  
  238. RCF:C_Progs/CanDoMJC/CanDoMJC 3811064 31 2 -2.9 2.9 -2.0 2.0
  239. J -0.5 0.0
  240.  
  241. The zeroeth parameter (as viewed by the C program) is the path
  242. and filename of the C program. The first parameter is the memory
  243. address of the Window data structure for the MJPlotPic card. This
  244. address is available through the CanDo system variable,
  245. WindowAddress. The second and third parameters are the values of
  246. MaxDwell and Resolution. The fourth through seventh parameters
  247. are the values representing the plot range. The eighth parameter
  248. is the plot type, M or J. Finally, the ninth and tenth
  249. parameters are the real and imaginary parts of the ci value used
  250. in Julia set plots. The C program is described below.
  251.     If the user just wants to view the last generated plot, then
  252. the file ram:CanDoMJPic, which is used to store the last plot
  253. (see below), is loaded into the MJPic buffer and displayed. Be
  254. aware that if no plot has yet been generated, the file
  255. ram:CanDoMJPic will not exist. In this case, CanDo will
  256. automatically display a file requester so the user can make a
  257. selection. Also, if you use TheMultiBinder to create a tool from
  258. this deck, do not include CanDoMJPic during the binding process.
  259. If you do, TheMultiBinder will add the current picture in this
  260. file to the bound deck. This prevents the program from loading
  261. the file each time it is to be displayed, thus always showing the
  262. same picture.
  263.  
  264. The NewRange Area Button
  265.     This button allows the user to click the left mouse button
  266. at any location on the screen, drag out a rectangle, and then
  267. release the button. By doing so, a new plotting range is set
  268. based on the rectangle boundaries. The button is 320x200 pixels
  269. in size and has no highlighting or border. Therefore, it covers
  270. the entire card and is invisible. The only reason for its
  271. existence is to detect mouse button clicks and movements. Neither
  272. the window object nor the card can handle mouse events. Thus, the
  273. entire card has to be covered with an invisible area button. The
  274. button has three scripts to allow it to function properly:
  275. OnClick, OnDrag, and OnRelease. OnClick executes when the left
  276. mouse button is pressed, OnDrag executes as the mouse is moved
  277. about while continuing to hold down the left mouse button, and
  278. OnRelease executes when the left mouse button is released.
  279.     The OnClick script determines the current location of the
  280. mouse from the system variables MouseX and MouseY. Also, the
  281. width and height of the rectangle are set to zero, the draw mode
  282. is set to COMPLEMENT, and an initial rectangle is drawn. As the
  283. mouse is moved, the OnDrag script executes over and over. It
  284. first draws another rectangle over top of the previously drawn
  285. rectangle. Since the draw mode is COMPLEMENT, this causes the
  286. previous rectangle to disappear. Next, the rectangle width and
  287. height are updated and another rectangle is drawn. This gives
  288. the appearance on screen that the rectangle is elastic and is
  289. stretching to match the mouse movements. This continues until
  290. the left mouse button is released. The OnRelease script first
  291. draws one final rectangle to erase the last one drawn. It then
  292. updates the real and imaginary plot ranges based on the
  293. boundaries of the rectangle. Next, the current picture is moved
  294. into buffer MJPic and saved to file ram:CanDoMJPic so that it can
  295. be recalled later if needed. Finally, card MJSettings is
  296. activated, passing an argument of "Reset" so the plot boundary
  297. fields will be updated. Be aware that the code only provides for
  298. the rectangle to be drawn from upper left to lower right. Trying
  299. to create the rectangle any other way will cause high and low
  300. values to be interchanged, thus producing upside down and
  301. backward plots.
  302.  
  303. The Options Menu
  304.     The MJPlotPic card has one menu structure named Options that
  305. contains three menu items. The menu structure looks like this:
  306.  
  307.     Options
  308.         Save Picture
  309.         Set Colors
  310.         To Settings
  311.  
  312. Each menu item has a shortcut key associated with it. They are
  313. shown in the definition sections of the menu objects in Listing
  314. 1.
  315.     When Save Picture is selected, a file requester is
  316. displayed, allowing the user to select a filename to which to
  317. save the current plot. This is useful if you would like to move
  318. the picture into a paint or image processing software package for
  319. modifications.
  320.     When Set Colors is selected, the ChangePalette card in the
  321. ColorChange deck is opened as a requester, allowing the user to
  322. modify the colors of the plot (see Figure 5). The ColorChange
  323. deck was described in another one of my articles (AC 9.5).
  324.     When To Settings is selected, the current plot is saved to
  325. the temporary file ram:CanDoMJPic for later recall, and the
  326. MJSettings card is activated.
  327.  
  328. The CanDoMJC C Program
  329.     This program consists of two source files: a header file
  330. named CanDoMJC.h and the main file named CanDoMJC.c. The former
  331. is shown in Listing 2, the latter in Listing 3.
  332.     CanDoMJC.h, which is #included by CanDoMJC.c, #includes
  333. several system header files, #defines some macro variables, and
  334. declares some useful structures. It also contains two functions
  335. for opening and closing the intuition and graphics system
  336. libraries.
  337.     CanDoMJC.c contains two functions in addition to the
  338. mandatory main function. The first is named MJPlot. It is used
  339. to plot the requested Mandelbrot or Julia set. If you are
  340. familiar with C programming, you will see that the code is very
  341. similar to the CanDo plotting code in the AfterAttachment script
  342. for the MJPlotPic card. In fact, the algorithms are exactly the
  343. same and will produce identical plots for the same settings. The
  344. second additional function is named SetComplexRange and simply
  345. fills in a ComplexRange data structure with four floating point
  346. values passed as parameters to the function.
  347.     The main function declares several variables, calls the
  348. OpenLibraries function (see CanDoMJC.h), and then reads and
  349. converts all of the parameter strings passed to the program from
  350. the CanDo deck. The first parameter is the address of the Window
  351. data structure for the MJPlotPic card. The string is converted
  352. to an unsigned long integer which in turn is converted to a
  353. pointer to a Window data structure. With this address, the C
  354. program can draw directly on the MJPlotPic card. The remaining
  355. parameters are as described earlier and have their data types
  356. converted appropriately. Once the parameters are read (note that
  357. the last two parameters are only read if a Julia set plot is
  358. requested), the Mandelbrot or Julia set is plotted. Afterwards,
  359. the CloseLibraries function (see CanDoMJC.h) is called and the
  360. program ends.
  361.     The CanDoMJC program was compiled as shown in the listings
  362. with no errors or warnings using the SAS/C Development System
  363. Version 6.50. The compiler options were set such that the program
  364. uses the IEEE floating point libraries supplied with AmigaDOS.
  365. These are the same libraries used by CanDo for its floating point
  366. calculations. Thus the CanDo and C code are on equal footing when
  367. it comes to the actual floating point calculations. The
  368. difference in speed comes from the C code being compiled whereas
  369. the CanDo code is only semi-compiled. Also, CanDo spends time
  370. converting between data types whereas C uses variables with
  371. predetermined data types.
  372.  
  373. Speed Comparisons
  374.     As suspected, the C code generated plots quite a bit faster
  375. than did the CanDo code. The plot times discussed below apply to
  376. my Amiga 2000 with a 68030/68882 28MHz GVP accelerator card.
  377.     In general, the C code executed about 40-50 times faster
  378. than the CanDo code. For instance, the Mandelbrot set shown in
  379. Figure 1 (maximum dwell of 31) took 50 seconds to generate with
  380. the C code and 41 minutes to generate with CanDo code. The Julia
  381. set in Figure 6 (maximum dwell of 63) took 55 seconds in C and 38
  382. minutes in CanDo. Thus, for floating point math intensive CanDo
  383. programs, it is advisable to write a C language support program
  384. to enhance the performance.
  385.  
  386. Other Front Ending Methods
  387.     CanDoMJ has a very simple link to its back end C program. It
  388. simply passes a window address along with some other numerical
  389. values to the C program. The C program does its thing and then
  390. exits. In other programs, there may need to be a two-way
  391. communication between the front end program and the C program. In
  392. this case, data could be transferred between programs via data
  393. files on the RAM: device. Or, if more sophisticated
  394. communications are needed, ARexx communications could be added to
  395. the C program. Of course, the authoring tool used for the front
  396. end would also need to support ARexx communications. CanDo does.
  397.  
  398. More Mandelbrot and Julia Set Information
  399.     If you have an interest in Mandelbrot or Julia sets, then
  400. you might want a subscription to AMYGDALA. This is a newsletter
  401. devoted to fractals, especially the Mandelbrot set, and is
  402. published by Rollo Silver, Box 219, San Cristobal, NM 85764. A
  403. 35mm slide supplement is also available.
  404.     There are many books on the market that discuss the
  405. Mandelbrot set, Julia sets, and fractals in general. Below is a
  406. list of several I own that you might find interesting.
  407.  
  408. Barnsley, Michael; Fractals Everywhere; Academic Press 1988.
  409. Barnsley, Michael F. and Hurd, Lyman P.; Fractal Image
  410. Compression; AK Peters 1993.
  411. Cvitanovic, Predrag; Universality in Chaos; Adam Hilger Ltd.
  412. 1984.
  413. Feder, Jens; Fractals; Plenum Press 1988.
  414. Gleick, James; Chaos: Making a New Science; Viking 1987.
  415. Mandelbrot, Benoit B.; The Fractal Geometry of Nature; W.H.
  416. Freeman & Co. 1983.
  417. Oliver, Dick; FractalVision: Put Fractals to Work for You; SAMS
  418. Publishing 1992.
  419. Peitgen, Heinz-Otto, Jurgens, Hartmut, and Saupe, Dietmar;
  420. Fractals for the Classroom: Part1 Introduction to Fractals
  421. and Chaos; Springer-Verlag 1992.
  422. Peitgen, H.-O. and Richter, P.H.; The Beauty of Fractals;
  423. Springer-Verlag 1986.
  424. Peitgen, H.-O. and Saupe, D.; The Science of Fractal Images;
  425. Springer-Verlag 1988.
  426. Sparrow, Colin; The Lorenz Equations: Bifurcations, Chaos, and
  427. Strange Attractors; Springer-Verlag 1982.
  428. Stevens, Roger T.; Fractal Programming in C; M&T Publishing, Inc.
  429. 1989.
  430. Thompson, J.M.T. and Stewart, H.B.; Nonlinear Dynamics and
  431. Chaos; John Wiley and Sons 1986.
  432. core. Once the score finishes playing,
  433. it will repeat. To stop the score, press the Stop button. A STOP
  434. command will be sent to Deluxe Music.
  435.  
  436. Enhancing the Scores
  437.     Since RandomNotes only produces a user constrained random
  438. score, most of the scores will not sound good. However, since
  439. they are so simple to create, many scores can be created in a
  440. short amount of time. Occasionally, one will sound fairly good.
  441. At this point, the user can interact directly with the Deluxe
  442. Music interface to modify the fairly good score to make it a very
  443. good or even excellent score, depending on the abilities of the
  444. user. In other words, RandomNotes should be viewed as an idea
  445. generator. It produces a score that helps plant a tune into the
  446. mind of the musician. The musician can then enhance the score to
  447. fit the idea.
  448.     If you are familiar with music theory, you might want to
  449. modify the algorithms in RandomNotes and create a program named
  450. MozartNotes, or BachNotes, or even MadonnaNotes. In other words,
  451. the program could take user defined parameters and create an
  452. original score that is reminiscent of the tunes written by or
  453. performed by a particular musical artist. If anyone creates such
  454. a program, I would be very interested in seeing it. Write to me
  455. care of this magazine.
  456.  y ⁿⁿ πτµ>╠τ╔ ?■|≤9 ?ƒ3?ƒ ┐ÖΣⁿ═7₧y╬ ╧ τ≤∩╧╧τƒ ╧╧■>~cτⁿ╬|ƒ≤ ∙ⁿ τ■yƒ² ■U`a┴Çü├âåéß┴c7└â ╟└aîα≡@`f╚aå80808`08!aüÄ1âp